package com.example.itshome.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.itshome.R;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.ChangeNicknameRequest;

public class ChangeNickNameActivity extends AppCompatActivity {

    private ImageView backImageView;
    private EditText nicknameEditText;
    private TextView confirmTextView;
    private TextView textCountTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_nick_name);
        initFind();
        initButton();
        initView();
    }

    private void initFind(){
        backImageView=findViewById(R.id.imv_change_nickname_page_back);
        nicknameEditText=findViewById(R.id.edt_change_nickname_page_nickname);
        confirmTextView=findViewById(R.id.txv_change_nickname_page_confirm);
        textCountTextView=findViewById(R.id.txv_change_nickname_page_text_count);
    }

    private void initButton(){
        backImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        confirmTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AbstractNetworkRequest request=new ChangeNicknameRequest(ChangeNickNameActivity.this)
                        .setNickname(nicknameEditText.getText().toString());
                request.start();
            }
        });
    }

    private void initView(){
        nicknameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.length()>20){
                    editable.delete(20,editable.length());
                }
                textCountTextView.setText(String.valueOf(editable.length()));
            }
        });
    }
}